package com.solcoo.controller;

import java.sql.SQLException;
import java.util.List;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.solcoo.dao.VersionDao;
import com.solcoo.pojo.Version;

@Controller
public class VersionController extends AbstractController {
    @Resource
    private VersionDao versionDao;
    
    private static Logger logger = Logger.getLogger(VersionController.class);
    
    @RequestMapping(value="version.do")
    public @ResponseBody JsonResult version(String version, String leixing) throws Exception {
		Version ver =  versionDao.queryVersion(leixing);
		String[] a = ver.getVersion().split("\\.");
		String[] b = version.split("\\.");
		for(int i=0;i<a.length;i++) {
			if(Integer.parseInt(a[i]) > Integer.parseInt(b[i])) {
				return getSuccessJsonResult(ver.getApppath()+"?t="+System.currentTimeMillis());
			}
			if(Integer.parseInt(a[i]) < Integer.parseInt(b[i])) {
				return getSuccessJsonResult("");
			}
		}
		return getSuccessJsonResult("");
    }
    
    public List<Version> getVersions() {
    	try {
    		return versionDao.queryVersions();
		} catch (SQLException e) {
			logger.error(e.getMessage(),e);
			return null;
		}
    }
    
    public void verUpdate(String grver, String shver) {
    	try {
    		if(grver!=null && !"".equals(grver)) {
    			versionDao.updateByLx(grver, "2");
    		}
    		if(shver!=null && !"".equals(shver)) {
    			versionDao.updateByLx(shver, "1");
    		}	
		} catch (Exception e) {
            logger.error(e.getMessage(),e);
		}
    }
}
